package top.zerozoo.learn.grade.factory;

import top.zerozoo.learn.grade.dao.*;
import top.zerozoo.learn.grade.dao.impl.*;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Dao工厂类
 * 要想获取一个dao的对象，应从本类的方法中获取
 */
public class DaoFactory {
    private static ConcurrentHashMap<Class, IDao> daoMap = new ConcurrentHashMap<>();

    static {
        init();
    }

    @SuppressWarnings("unchecked")
    public static <T extends IDao> T get(Class<T> clazz) {
        return (T) daoMap.get(clazz);
    }

    protected static void init() {
        ConcurrentHashMap<Class, IDao> map = new ConcurrentHashMap<>();
        map.put(AdminDao.class, new AdminDaoImpl());
        map.put(MajorDao.class, new MajorDaoImpl());
        map.put(CourseScoreDao.class, new CourseScoreDaoImpl());
        map.put(StudentBaseInfoDao.class, new StudentBaseInfoDaoImpl());
        map.put(StudentDao.class, new StudentDaoImpl());
        daoMap = map;
    }

}
